/*
Java source file
Copyright: Umschalt.com 2012
*/
package com.david.documentation.service.tag;

import com.david.documentation.domain.dao.tags.TagDao;
import com.david.documentation.domain.entities.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service(value = "tagService")
@Transactional(rollbackFor = Exception.class)
public class TagServiceImpl implements TagService {

	@Autowired
	private TagDao tagDao;

	public Tag findById(Long id) {
		return tagDao.findById(id);
	}

	public void save(Tag tag) {
		tagDao.merge(tag);
	}

	public Tag update(Tag tag) {
		return tagDao.merge(tag);
	}

	public void remove(Tag tag) {
		tagDao.removeSafely(tag);
	}

	public long size() {
		return tagDao.size();
	}

	public List<Tag> findAll() {
		return tagDao.findAll();
	}

	public boolean checkforDublicate(String name){
		return tagDao.checkforDublicate(name);
	}

	public Number getNumberOfDokus(Long tagId) {
		return tagDao.getNumberOfDokus(tagId);
	}

	public List<Tag> findTagsByName(String name) {
		return tagDao.findTagsByName(name);
	}

	public boolean tagExists(String name) {
		List<String> tags = new ArrayList<String>();
		for (Tag tmp : findAll()){
			if (!tags.contains(name)){
				tags.add(tmp.getName());
			}
		}
		return tags.contains(name);
	}

	public Tag findTagByName(String name) {
		return tagDao.findByProperty("name",name);
	}
}
